import { Component, Input, OnInit } from '@angular/core';
import { Router } from '@angular/router'
import { Location } from '@angular/common'

import { User } from "./user"
import { UserService } from "./user.service"

@Component({
  selector: "left-nav",
  templateUrl: './left-nav.component.html',
  styleUrls: [ "./left-nav.component.css" ]
})
export class LeftNavComponent implements OnInit {
  user: User = new User()
  path: string

  constructor(
    private userService: UserService,
    private location: Location,
    private router: Router) { }

  ngOnInit(): void {
    this.path = this.location.path()
    this.userService.getUserMine().then(user => {
      if (this.path != "/login" && !user)
        return this.router.navigate([ "login" ])
      this.user = user
    })
  }

  changePart(path: Array<string>): void {
    this.path = "/" + path.join("/")
    this.router.navigate(path)
  }

  login(): void {
    this.router.navigate(["login"])
  }

  logout(): void {
    localStorage.setItem("token", null)
    this.user = null
    UserService.MINE = null
    this.router.navigate([ "login" ])
  }

  needLogin(): void {
    alert("您还没有登录！请先登录")
    this.router.navigate([ "login" ])
  }
}
